package cn.powerone.scm.service.impl;

import java.util.Date;
import java.util.List;

import cn.powerone.scm.domain.ScmCustomerOrders;
import cn.powerone.scm.domain.ScmSupplierOrders;
import cn.powerone.scm.pojo.bo.ScmProjectCommonReq;
import cn.powerone.scm.service.IScmCustomerOrdersService;
import cn.powerone.scm.service.IScmSupplierOrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.powerone.scm.mapper.ScmInventoryLogMapper;
import cn.powerone.scm.domain.ScmInventoryLog;
import cn.powerone.scm.service.IScmInventoryLogService;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author PowerOne
 * @date 2025-08-21
 */
@Service
public class ScmInventoryLogServiceImpl implements IScmInventoryLogService
{
    @Autowired
    private ScmInventoryLogMapper scmInventoryLogMapper;

    @Autowired
    private IScmSupplierOrdersService scmSupplierOrdersService;

    @Autowired
    private IScmCustomerOrdersService scmCustomerOrdersService;
    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public ScmInventoryLog selectScmInventoryLogById(Long id)
    {
        return scmInventoryLogMapper.selectScmInventoryLogById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param scmInventoryLog 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<ScmInventoryLog> selectScmInventoryLogList(ScmInventoryLog scmInventoryLog)
    {
        //        ScmSupplierOrders scmSupplierOrders;
//        ScmCustomerOrders scmCustomerOrders;
//        for (int i = 0; i < list.size(); i++){//遍历list,并修改每一个orderDate
//            if (list.get(i).getChangeType() == 1){ //1:入库
//                scmSupplierOrders = scmSupplierOrdersService.selectScmSupplierOrdersById(list.get(i).getOrderId());
//                if (scmSupplierOrders != null)
//                    list.get(i).setOrderDate(scmSupplierOrders.getOrderDate());
//            }
//            if (list.get(i).getChangeType() == 2){ //2:出库
//                scmCustomerOrders = scmCustomerOrdersService.selectScmCustomerOrdersById(list.get(i).getOrderId());
//                if (scmCustomerOrders != null)
//                    list.get(i).setOrderDate(scmCustomerOrders.getOrderDate());
//            }
//
//        }

        return scmInventoryLogMapper.selectScmInventoryLogList(scmInventoryLog);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param scmInventoryLog 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertScmInventoryLog(ScmInventoryLog scmInventoryLog)
    {
        return scmInventoryLogMapper.insertScmInventoryLog(scmInventoryLog);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param scmInventoryLog 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateScmInventoryLog(ScmInventoryLog scmInventoryLog)
    {
        return scmInventoryLogMapper.updateScmInventoryLog(scmInventoryLog);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteScmInventoryLogByIds(Long[] ids)
    {
        return scmInventoryLogMapper.deleteScmInventoryLogByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteScmInventoryLogById(Long id)
    {
        return scmInventoryLogMapper.deleteScmInventoryLogById(id);
    }

    /**
     * 基于项目，查询【进出库log list】
     *
     * @param scmProjectCommonReq 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<ScmInventoryLog> selectScmInventoryLogListByProject(ScmProjectCommonReq scmProjectCommonReq) {
        if (scmProjectCommonReq.getChangeType() == null){
            throw new RuntimeException("输入数据错误!");
        }
        List<ScmInventoryLog> outputList = null;
        if (scmProjectCommonReq.getChangeType() == 1){
            ScmSupplierOrders scmSupplierOrdersInput = new ScmSupplierOrders();
            scmSupplierOrdersInput.setProjectId(scmProjectCommonReq.getProjectId());
            List<ScmSupplierOrders> list = scmSupplierOrdersService.selectScmSupplierOrdersList(scmSupplierOrdersInput);
            Long[] orderIds = list.stream().map(ScmSupplierOrders::getId).toArray(Long[]::new);
            if (orderIds.length == 0) {
                //返回空列表
                return List.of();
            }
            outputList = scmInventoryLogMapper.selectScmInventoryLogListByOrders(orderIds);

            // 创建一个Map来存储orderId和orderDate的映射关系
            java.util.Map<Long, java.util.Date> orderDateMap = new java.util.HashMap<>();
            for (ScmSupplierOrders order : list) {
                orderDateMap.put(order.getId(), order.getOrderDate());
            }

            // 从map中获得id一样的orderDate
            for (ScmInventoryLog scmInventoryLog : outputList){
                scmInventoryLog.setOrderDate(orderDateMap.get(scmInventoryLog.getOrderId()));
            }
            return outputList;
        }
        if (scmProjectCommonReq.getChangeType() == 2){
            ScmCustomerOrders scmCustomerOrdersInput = new ScmCustomerOrders();
            scmCustomerOrdersInput.setProjectId(scmProjectCommonReq.getProjectId());
            List<ScmCustomerOrders> list = scmCustomerOrdersService.selectScmCustomerOrdersList(scmCustomerOrdersInput);
            Long[] orderIds = list.stream().map(ScmCustomerOrders::getId).toArray(Long[]::new);
            if (orderIds.length == 0) {
                //返回空列表
                return List.of();
            }
            outputList = scmInventoryLogMapper.selectScmInventoryLogListByOrders(orderIds);
            
            // 创建一个Map来存储orderId和orderDate的映射关系
            java.util.Map<Long, java.util.Date> orderDateMap = new java.util.HashMap<>();
            for (ScmCustomerOrders order : list) {
                orderDateMap.put(order.getId(), order.getOrderDate());
            }
            
            // 从map中获得id一样的orderDate
            for (ScmInventoryLog scmInventoryLog : outputList){
                scmInventoryLog.setOrderDate(orderDateMap.get(scmInventoryLog.getOrderId()));
            }
            return outputList;
        }
        throw new RuntimeException("输入数据错误!");
    }

    @Override
    public int selectChangeQuantityInCountByMonth(Date yesterday, int changeType) {
        return scmInventoryLogMapper.selectChangeQuantityInCountByMonth(yesterday, (long) changeType);
    }

    @Override
    public int selectChangeQuantityInCountByYear(Date yesterday, int changeType) {
        return scmInventoryLogMapper.selectChangeQuantityInCountByYear(yesterday, (long) changeType);
    }

    /**
     * 基于客户或者供应商，查询【进出库log list】
     *
     * @param scmProjectCommonReq 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<ScmInventoryLog> selectScmInventoryLogListByCustomerOrSupplier(ScmProjectCommonReq scmProjectCommonReq) {
        List<ScmInventoryLog> outputList;
        if (scmProjectCommonReq.getChangeType() == 1){
            ScmSupplierOrders scmSupplierOrdersInput = new ScmSupplierOrders();
            scmSupplierOrdersInput.setSupplierId(scmProjectCommonReq.getSupplierId());
            List<ScmSupplierOrders> list = scmSupplierOrdersService.selectScmSupplierOrdersList(scmSupplierOrdersInput);
            Long[] orderIds = list.stream().map(ScmSupplierOrders::getId).toArray(Long[]::new);
            if (orderIds.length == 0) {
                //返回空列表
                return List.of();
            }
            outputList = scmInventoryLogMapper.selectScmInventoryLogListByOrders(orderIds);

            // 创建一个Map来存储orderId和orderDate的映射关系
            java.util.Map<Long, java.util.Date> orderDateMap = new java.util.HashMap<>();
            for (ScmSupplierOrders order : list) {
                orderDateMap.put(order.getId(), order.getOrderDate());
            }

            // 从map中获得id一样的orderDate
            for (ScmInventoryLog scmInventoryLog : outputList){
                scmInventoryLog.setOrderDate(orderDateMap.get(scmInventoryLog.getOrderId()));
            }
            return outputList;
        }
        if (scmProjectCommonReq.getChangeType() == 2){
            ScmCustomerOrders scmCustomerOrdersInput = new ScmCustomerOrders();
            scmCustomerOrdersInput.setCustomerId(scmProjectCommonReq.getCustomerId());
            List<ScmCustomerOrders> list = scmCustomerOrdersService.selectScmCustomerOrdersList(scmCustomerOrdersInput);
            Long[] orderIds = list.stream().map(ScmCustomerOrders::getId).toArray(Long[]::new);
            if (orderIds.length == 0) {
                //返回空列表
                return List.of();
            }
            outputList = scmInventoryLogMapper.selectScmInventoryLogListByOrders(orderIds);

            // 创建一个Map来存储orderId和orderDate的映射关系
            java.util.Map<Long, java.util.Date> orderDateMap = new java.util.HashMap<>();
            for (ScmCustomerOrders order : list) {
                orderDateMap.put(order.getId(), order.getOrderDate());
            }

            // 从map中获得id一样的orderDate
            for (ScmInventoryLog scmInventoryLog : outputList){
                scmInventoryLog.setOrderDate(orderDateMap.get(scmInventoryLog.getOrderId()));
            }
            return outputList;
        }
        throw new RuntimeException("输入数据错误!");
    }
}
